﻿Ext.define("PSI.OutPlan.SendTruckForm", {
    createUI: function (entity) {
        var me = this;
        me.entity = entity;

        var wnd = Ext.create("Ext.window.Window", {
            title: "派车",
            modal: true,
            onEsc: Ext.emptyFn,
            width: 500,
            height: 430,
            layout: "fit",
            items: [
                {
                    id: "editForm",
                    xtype: "form",
                    layout: "form",
                    height: "100%",
                    bodyPadding: 5,
                    defaultType: 'textfield',
                    fieldDefaults: {
                        labelWidth: 80,
                        labelAlign: "right",
                        labelSeparator: "",
                        msgTarget: 'side'
                    },
                    items: [
                        {
                            xtype: "hidden",
                            name: "id",
                            value: entity.id
                        },
                        {
                            xtype: "displayfield",
                            value: entity.planNumber,
                            fieldLabel: "计划号"
                        },
                        {
                            xtype: "displayfield",
                            value: entity.address,
                            fieldLabel: "门点"
                        },
                        {
                            xtype: "displayfield",
                            id: "editTakeCntDT",
                            value: entity.takeCntDT,
                            fieldLabel: "装箱时限"
                        },
                        {
                            id: "editTruckCompany",
                            name: "truckCompany",
                            fieldLabel: "拖车公司",
                            xtype: "textfield",
                            readOnly: true
                        },
                        {
                            id: "editTruckNumber",
                            xtype: "psi_truck_trucknumberfield",
                            name: "truckNumber",
                            fieldLabel: "车号",
                            allowBlank: false,
                            blankText: "没有输入车号",
                            beforeLabelTextTpl: PSI.Const.REQUIRED
                        },
                        {
                            id: "editDriver",
                            xtype: "textfield",
                            name: "driver",
                            fieldLabel: "司机"
                        }, {
                            id: "editDriverTel",
                            xtype: "textfield",
                            name: "driverTel",
                            fieldLabel: "司机电话"
                        }, {
                            xtype: "fieldcontainer",
                            fieldLabel: "计划到装货地时间",
                            beforeLabelTextTpl: PSI.Const.REQUIRED,

                            layout: "hbox",
                            items: [
                                {
                                    id: "editDT0Date",
                                    xtype: "datefield",
                                    format: "Y-m-d",
                                    allowBlank: false,
                                    blankText: "没有输入装箱日期",
                                    flex: 2,
                                    name: "dt0Date"
                                }, {
                                    id: "editDT0Time",
                                    xtype: "timefield",
                                    format: "H:i",
                                    allowBlank: false,
                                    blankText: "没有输入装箱时间",
                                    flex: 2,
                                    name: "dt0Time"
                                }, {
                                    id: "editDT0btn",
                                    xtype: "button",
                                    flex: 1,
                                    text:'当前时间'
                                }
                            ]
                        }, {
                            xtype: "fieldcontainer",
                            fieldLabel: "计划装箱开始时间",
                          //  beforeLabelTextTpl: PSI.Const.REQUIRED,
                            layout: "hbox",
                            items: [
                                {
                                    id: "editDT1Date",
                                    xtype: "datefield",
                                    format: "Y-m-d",
                                    //allowBlank: false,
                                    //blankText: "没有输入装箱日期",
                                    flex: 2,
                                    name: "dt1Date"
                                }, {
                                    id: "editDT1Time",
                                    xtype: "timefield",
                                    format: "H:i",
                                    //allowBlank: false,
                                    //blankText: "没有输入装箱时间",
                                    flex: 2,
                                    name: "dt1Time"
                                }, {
                                    id: "editDT1btn",
                                    xtype: "button",
                                    flex: 1,
                                    text: '当前时间'
                                }

                            ]
                        }, {
                            xtype: "fieldcontainer",
                            fieldLabel: "计划装箱完成时间",
                            layout: "hbox",
                            items: [
                                {
                                    id: "editDT2Date",
                                    xtype: "datefield",
                                    format: "Y-m-d",
                                    flex: 2,
                                    name: "dt2Date"
                                }, {
                                    id: "editDT2Time",
                                    xtype: "timefield",
                                    format: "H:i",
                                    flex: 2,
                                    name: "dt2Time"
                                }, {
                                    id: "editDT2btn",
                                    xtype: "button",
                                    flex: 1,
                                    text: '当前时间'
                                }
                            ]
                        }, {
                            xtype: "fieldcontainer",
                            fieldLabel: "计划回港时间",
                            layout: "hbox",
                            items: [
                                {
                                    id: "editDT3Date",
                                    xtype: "datefield",
                                    format: "Y-m-d",
                                    flex: 2,
                                    name: "dt3Date"
                                }, {
                                    id: "editDT3Time",
                                    xtype: "timefield",
                                    format: "H:i",
                                    flex: 2,
                                    name: "dt3Time"
                                }, {
                                    id: "editDT3btn",
                                    xtype: "button",
                                    flex: 1,
                                    text: '当前时间'
                                }
                            ]
                        }, {
                            xtype: "fieldcontainer",
                            fieldLabel: "计划完成时间",
                            layout: "hbox",
                          //  beforeLabelTextTpl: PSI.Const.REQUIRED,
                            items: [
                                {
                                    id: "editDT4Date",
                                    xtype: "datefield",
                                    format: "Y-m-d",
                                    //allowBlank: false,
                                    //blankText: "没有输入完成日期",
                                    flex: 2,
                                    name: "dt4Date"
                                }, {
                                    id: "editDT4Time",
                                    xtype: "timefield",
                                    format: "H:i",
                                    //allowBlank: false,
                                    //blankText: "没有输入完成时间",
                                    flex: 2,
                                    name: "dt4Time"
                                }, {
                                    id: "editDT4btn",
                                    xtype: "button",
                                    flex: 1,
                                    text: '当前时间'
                                }
                            ]
                        }, {
                            xtype: "psi_outplan_tripnumberfield",
                            id: "editTripNumber",
                            name: "tripNumber",
                            fieldLabel: "趟次号",
                            allowBlank: false,
                            blankText: "没有输入趟次号",
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            value: entity.planNumber
                        }
                    ],
                    buttons: [
                        {
                            text: "确定",
                            formBind: true,
                            iconCls: "psi-button-ok",
                            handler: me.onOK,
                            scope: me
                        },
                        {
                            text: "取消", handler: function () {
                                PSI.MsgBox.confirm("请确认是否取消操作?", function () {
                                    me.wnd.close();
                                });
                            }, scope: me
                        }
                    ]
                }
            ]

        });

        me.wnd = wnd;
        var dat = new Date();
        var cc = Ext.Date.format(dat, 'Y-m-d');
        var ss = Ext.Date.format(dat, 'H:i');
        Ext.getCmp("editTripNumber").setTruckNumberEditor(Ext.getCmp("editTruckNumber"));

        Ext.getCmp("editDT0btn").on("click", function () {  
            Ext.getCmp("editDT0Date").setValue(cc);
            Ext.getCmp("editDT0Time").setValue(ss);
        }, me)

        Ext.getCmp("editDT1btn").on("click", function () {
            Ext.getCmp("editDT1Date").setValue(cc);
            Ext.getCmp("editDT1Time").setValue(ss);
        }, me)

        Ext.getCmp("editDT2btn").on("click", function () {
            Ext.getCmp("editDT2Date").setValue(cc);
            Ext.getCmp("editDT2Time").setValue(ss);
        }, me)

        Ext.getCmp("editDT3btn").on("click", function () {
            Ext.getCmp("editDT3Date").setValue(cc);
            Ext.getCmp("editDT3Time").setValue(ss);
        }, me)

        Ext.getCmp("editDT4btn").on("click", function () {
            Ext.getCmp("editDT4Date").setValue(cc);
            Ext.getCmp("editDT4Time").setValue(ss);
        }, me)

        Ext.getCmp("editTruckNumber").setLinkedEditor({
            truckCompanyEditor: Ext.getCmp("editTruckCompany"),
            driverEditor: Ext.getCmp("editDriver"),
            driverTelEditor: Ext.getCmp("editDriverTel")
        });

        Ext.getCmp("editTruckCompany").on("specialkey", function (field, e) {
            if (e.getKey() == e.BACKSPACE) {
                e.preventDefault();
            }
        }, me);

        Ext.getCmp("editTruckNumber").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT0Date").focus();
            }
        }, me);
        Ext.getCmp("editDT0Date").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT0Time").focus();
            }
        }, me);
        Ext.getCmp("editDT0Time").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT1Date").focus();
            }
        }, me);
        Ext.getCmp("editDT1Date").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT1Time").focus();
            }
        }, me);
        Ext.getCmp("editDT1Time").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT2Date").focus();
            }
        }, me);
        Ext.getCmp("editDT2Date").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT2Time").focus();
            }
        }, me);
        Ext.getCmp("editDT2Time").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT3Date").focus();
            }
        }, me);
        Ext.getCmp("editDT3Date").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT3Time").focus();
            }
        }, me);
        Ext.getCmp("editDT3Time").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT4Date").focus();
            }
        }, me);
        Ext.getCmp("editDT4Date").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editDT4Time").focus();
            }
        }, me);
        Ext.getCmp("editDT4Time").on("specialkey", function (field, e) {
            if (e.getKey() == e.ENTER) {
                Ext.getCmp("editTripNumber").focus();
            }
        }, me);
    },

    show: function (parentForm, entity) {
        var me = this;

        me.parentForm = parentForm;

        me.createUI(entity);

        me.wnd.on("show", function () { me.onEditFormShow(entity); }, me);

        me.wnd.show();
    },

    onEditFormShow: function (entity) {
        Ext.getCmp("editTruckNumber").focus();

        var me = this;
        var form = me.wnd;
        var el = me.wnd.getEl() || Ext.getBody();

        el.mask("数据加载中...");
        Ext.Ajax.request({
            url: "/OutPlan/GetSendTruckInfo",
            method: "POST",
            params: { id: entity.id },
            callback: function (options, success, response) {
                el.unmask();
                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    me.setEntityData(data);
                } else {
                    PSI.MsgBox.showInfo("网络错误");
                }
            }
        });
    },

    setEntityData: function (data) {
        Ext.getCmp("editTruckCompany").setValue(data.truckCompany);
        Ext.getCmp("editTruckNumber").setValue(data.truckNumber);
        Ext.getCmp("editDriver").setValue(data.driver);
        Ext.getCmp("editDriverTel").setValue(data.driverTel);
        if (data.tripNumber) {
            Ext.getCmp("editTripNumber").setValue(data.tripNumber);
        } else {
            Ext.getCmp("editTripNumber").setValue(this.entity.planNumber);
        }

        Ext.getCmp("editDT0Date").setValue(data.dt0PlanDate);
        Ext.getCmp("editDT0Time").setValue(data.dt0PlanTime);
        Ext.getCmp("editDT1Date").setValue(data.dt1PlanDate);
        Ext.getCmp("editDT1Time").setValue(data.dt1PlanTime);
        Ext.getCmp("editDT2Date").setValue(data.dt2PlanDate);
        Ext.getCmp("editDT2Time").setValue(data.dt2PlanTime);
        Ext.getCmp("editDT3Date").setValue(data.dt3PlanDate);
        Ext.getCmp("editDT3Time").setValue(data.dt3PlanTime);
        Ext.getCmp("editDT4Date").setValue(data.dt4PlanDate);
        Ext.getCmp("editDT4Time").setValue(data.dt4PlanTime);
    },

    // private
    onOK: function () {
        var me = this;

        var dt0 = me.getDT0();
        if (dt0 == null) {
            PSI.MsgBox.showInfo("没有输入计划到装货地时间");
            return;
        }
        var dt1 = me.getDT1();
        if (dt1 != null) {

            if (dt1 < dt0) {
                PSI.MsgBox.showInfo("装箱开始时间不能早于到装货地时间");
                return;
            }
            var dt2 = me.getDT2();
            if (dt2) {
                if (dt2 < dt1) {
                    PSI.MsgBox.showInfo("装箱完成时间不能早于装箱开始时间");
                    return;
                }
            }
        }
        var dt3 = me.getDT3();
        if (dt3) {
            if (dt2 && dt3 < dt2) {
                PSI.MsgBox.showInfo("回港时间不能早于装箱完成时间");
                return;
            } else if (dt3 < dt1) {
                PSI.MsgBox.showInfo("到达时间不能早于装箱开始时间");
                return;
            } else if (dt3 < dt0) {
                PSI.MsgBox.showInfo("到达时间不能早于到装货地时间");
                return;
            }
        }
        var dt4 = me.getDT4();
        //if (dt4 == null) {
        //    PSI.MsgBox.showInfo("没有输入计划完成时间");
        //    return;
        //}
        if (dt4 != null) {
            if (dt3 && dt4 < dt3) {
                PSI.MsgBox.showInfo("完成时间不能早于回港时间");
                return;
            }
            if (dt2 && dt4 < dt2) {
                PSI.MsgBox.showInfo("完成时间不能早于装箱完成时间");
                return;
            }
            if (dt4 < dt1) {
                PSI.MsgBox.showInfo("完成时间不能早于装箱开始时间");
                return;
            }
            if (dt4 < dt0) {
                PSI.MsgBox.showInfo("完成时间不能早于到装货地时间");
                return;
            }
        }

        var funcSubmit = function () {
            var f = Ext.getCmp("editForm");
            var el = f.getEl() || Ext.getBody();
            el.mask("数据保存中...");
            f.submit({
                url: "/OutPlan/SendTruck",
                method: "POST",
                success: function (form, action) {
                    el.unmask();
                    me.wnd.close();
                    me.parentForm.refreshPlanGrid();
                },
                failure: function (form, action) {
                    el.unmask();
                    PSI.MsgBox.showInfo(action.result.msg, function () {
                        Ext.getCmp("editDispatcher").focus();
                    });
                }
            });
        }

        var info = "请确认是否派车?";
        //if (dt1 > me.getTakeCntDT()) {
        //    info = "请确认是否派车?<br/><span style='color:red'>注意</span>: 计划装箱时间超过了装箱时限";
        //}

        PSI.MsgBox.confirm(info, funcSubmit);
    },
    getDT0: function () {
        var d = Ext.getCmp("editDT0Date").getValue();
        var t = Ext.getCmp("editDT0Time").getValue();
        if (d == null || t == null) {
            return null;
        }

        var s = Ext.Date.format(d, "Y-m-d") + " " + Ext.Date.format(t, "H:i");
        return Ext.Date.parse(s, "Y-m-d H:i");
    },

    getDT1: function () {
        var d = Ext.getCmp("editDT1Date").getValue();
        var t = Ext.getCmp("editDT1Time").getValue();
        if (d == null || t == null) {
            return null;
        }

        var s = Ext.Date.format(d, "Y-m-d") + " " + Ext.Date.format(t, "H:i");
        return Ext.Date.parse(s, "Y-m-d H:i");
    },

    getDT2: function () {
        var d = Ext.getCmp("editDT2Date").getValue();
        var t = Ext.getCmp("editDT2Time").getValue();
        if (d == null || t == null) {
            return null;
        }

        var s = Ext.Date.format(d, "Y-m-d") + " " + Ext.Date.format(t, "H:i");
        return Ext.Date.parse(s, "Y-m-d H:i");
    },

    getDT3: function () {
        var d = Ext.getCmp("editDT3Date").getValue();
        var t = Ext.getCmp("editDT3Time").getValue();
        if (d == null || t == null) {
            return null;
        }

        var s = Ext.Date.format(d, "Y-m-d") + " " + Ext.Date.format(t, "H:i");
        return Ext.Date.parse(s, "Y-m-d H:i");
    },

    getDT4: function () {
        var d = Ext.getCmp("editDT4Date").getValue();
        var t = Ext.getCmp("editDT4Time").getValue();
        if (d == null || t == null) {
            return null;
        }

        var s = Ext.Date.format(d, "Y-m-d") + " " + Ext.Date.format(t, "H:i");
        return Ext.Date.parse(s, "Y-m-d H:i");
    },

    getTakeCntDT: function () {
        var v = Ext.getCmp("editTakeCntDT").getValue();
        return Ext.Date.parse(v, "Y-m-d H:i");
    }
});